import { checkUrl } from '@/sheep/utils/checkUrlSuffix';
import { ElNotification } from 'element-plus';
import $store from '@/sheep/store';

/**
 *
 * @param {Date} timestamp -时间戳
 * @param {Number} index - 当前消息下标
 *
 */
/**
 * 状态回调
 * @param {*} res -接口返回数据
 * @param {Boolean} flag - false 默认关闭
 */
export function callBackNotice(res, flag = false) {
  flag &&
    res.msg &&
    ElNotification({
      title: 'socket',
      message: `客服错误：${res.msg}`,
      showClose: true,
      type: res.error == 0 ? 'success' : 'warning',
      duration: 3000,
    });
}

/**
 * xss过滤
 * @param {*} html -输入
 */
import xss from 'xss';
export function xssFilter(html) {
  const options = {
    whiteList: {
      img: ['src', 'name', 'style', 'class', 'width', 'height', 'id'], //img标签保留属性
      br: [],
    },
  };
  const myxss = new xss.FilterXSS(options);
  return myxss.process(html);
}

/**
 * 数据格式
 * {
     from:'', // 用户customer左 |  顾客customer_service右 | 系统system中间
     mode:'', // goods,order,image,text,system
     date:'', //时间
     content:{} // 内容
 }
 */
/**
 * 格式化发送信息
 * @param {*} message
 * @returns  obj - 消息对象
 */

export function formatInput(message) {
  let obj = {};
  switch (message.mode) {
    case 'text':
      const appInfo = $store('app').info;
      let reg = new RegExp('src="' + appInfo.domainurl, 'g');
      obj = {
        message_type: 'text',
        // message: xssFilter(message.content.text.replace(reg, 'src=\"'))
        message: message.content.text,
      };
      break;
    case 'image':
      obj = {
        message_type: 'image',
        message: message.content.path,
      };
      break;
    case 'goods':
      obj = {
        message_type: 'goods',
        message: message.content.item,
      };
      break;
    case 'order':
      obj = {
        message_type: 'order',
        message: message.content.item,
      };
      break;
    default:
      break;
  }
  return obj;
}
/**
 * 格式化客服接收信息
 * @param {*} message
 * @returns obj - 消息对象
 */
export function formatMessage(message) {
  let obj = {};
  switch (message.message_type) {
    case 'system':
      obj = {
        from: 'system',
        mode: 'system',
        date: message.create_time * 1000, //时间
        sender: message.sender,
        content: {
          text: message.message,
        },
      };
      break;
    case 'text':
      const appInfo = $store('app').info;
      let reg = 'src="' + appInfo.domainurl;
      let str = message.message;
      str = str.replace(/https:\/\/[^/]+/g, '');
      obj = {
        from: message.sender_identify,
        mode: message.message_type,
        date: message.create_time * 1000, //时间
        sender: message.sender,
        content: {
          text: str.replace(/(src=\")/g, reg),
          messageId: message.id,
        },
      };
      break;
    case 'image':
      obj = {
        from: message.sender_identify,
        mode: message.message_type,
        date: message.create_time * 1000, //时间
        sender: message.sender,
        content: {
          url: checkUrl(message.message),
          messageId: message.id,
        },
      };
      break;
    case 'goods':
      obj = {
        from: message.sender_identify,
        mode: message.message_type,
        date: message.create_time * 1000, //时间
        sender: message.sender,
        content: {
          item: message.message,
          messageId: message.id,
        },
      };
      break;
    case 'order':
      obj = {
        from: message.sender_identify,
        mode: message.message_type,
        date: message.create_time * 1000, //时间
        sender: message.sender,
        content: {
          item: message.message,
          messageId: message.id,
        },
      };
      break;
    default:
      break;
  }
  return obj;
}

/**
 * file 转换为 img
 * @param {*} file  - file 文件
 * @returns  img   - img标签
 */
export function readImg(file) {
  return new Promise((resolve, reject) => {
    const img = new Image();
    const reader = new FileReader();
    reader.onload = function (e) {
      img.src = e.target.result;
    };
    reader.onerror = function (e) {
      reject(e);
    };
    reader.readAsDataURL(file);
    img.onload = function () {
      resolve(img);
    };
    img.onerror = function (e) {
      reject(e);
    };
  });
}

/**
 * 压缩图片
 *@param img -被压缩的img对象
 * @param type -压缩后转换的文件类型
 * @param mx -触发压缩的图片最大宽度限制
 * @param mh -触发压缩的图片最大高度限制
 * @returns blob - 文件流
 */
export function compressImg(img, type = 'image/jpeg', mx = 1000, mh = 1000, quality = 1) {
  return new Promise((resolve, reject) => {
    const canvas = document.createElement('canvas');
    const context = canvas.getContext('2d');
    const { width: originWidth, height: originHeight } = img;
    // 最大尺寸限制
    const maxWidth = mx;
    const maxHeight = mh;
    // 目标尺寸
    let targetWidth = originWidth;
    let targetHeight = originHeight;
    if (originWidth > maxWidth || originHeight > maxHeight) {
      if (originWidth / originHeight > 1) {
        // 宽图片
        targetWidth = maxWidth;
        targetHeight = Math.round(maxWidth * (originHeight / originWidth));
      } else {
        // 高图片
        targetHeight = maxHeight;
        targetWidth = Math.round(maxHeight * (originWidth / originHeight));
      }
    }
    canvas.width = targetWidth;
    canvas.height = targetHeight;
    context.clearRect(0, 0, targetWidth, targetHeight);
    // 图片绘制
    context.drawImage(img, 0, 0, targetWidth, targetHeight);
    canvas.toBlob(
      function (blob) {
        resolve(blob);
      },
      type,
      quality,
    );
  });
}

export default {
  callBackNotice,
  formatInput,
  formatMessage,
  readImg,
  compressImg,
};
